<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年级" prop="type">
        <el-select v-model="queryParams.grade" placeholder="请选择年级" clearable size="small">
          <el-option  v-for="dict in gradeOptions"
                      :key="dict.dictValue"
                      :label="dict.dictLabel"
                      :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="学院" prop="type">
        <el-select v-model="queryParams.college" placeholder="请选择学院" clearable size="small" @change="onSelectedMajor">
          <el-option  v-for="dict in collegeOptions"
                      :key="dict.dictValue"
                      :label="dict.dictLabel"
                      :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="专业" prop="type">
        <el-select v-model="queryParams.major" placeholder="请选择专业" clearable size="small">
          <el-option  v-for="dict in majorOptions"
                      :key="dict.dictValue"
                      :label="dict.dictLabel"
                      :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="请假类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择请假类型" clearable size="small">
          <el-option  v-for="dict in typeOptions"
                     :key="dict.dictValue"
                     :label="dict.dictLabel"
                     :value="dict.dictValue" />
        </el-select>
      </el-form-item>


      <el-form-item label="导员审批" prop="approveStatusGuide">
        <el-select v-model="queryParams.approveStatusGuide" placeholder="请选择请假类型" clearable size="small">
          <el-option  v-for="dict in auditStatusOptions"
                      :key="dict.dictValue"
                      :label="dict.dictLabel"
                      :value="dict.dictValue" />
        </el-select>
      </el-form-item>

      <el-form-item label="领导审批" prop="approveStatusLeading">
        <el-select v-model="queryParams.approveStatusLeading" placeholder="请选择请假类型" clearable size="small">
          <el-option  v-for="dict in auditStatusOptions"
                      :key="dict.dictValue"
                      :label="dict.dictLabel"
                      :value="dict.dictValue" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:manage:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:manage:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:manage:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="manageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID主键" align="center" prop="leaveId" v-if="false"/>
      <el-table-column label="学号" align="center" prop="studentId" />
      <el-table-column label="班级信息" width="200" align="center">
        <template slot-scope="scope"> {{scope.row.grade}}级{{scope.row.college}}{{scope.row.major}} </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="userName" />
      <el-table-column width="100" label="请假类型" align="center">
        <template slot-scope="scope">
          <el-tex effect="dark" v-if="scope.row.type==0">事假</el-tex>
          <el-tex effect="dark" v-if="scope.row.type==1">病假</el-tex>
          <el-tex effect="dark" v-if="scope.row.type==2">实习</el-tex>
          <el-tex effect="dark" v-if="scope.row.type==2">其他</el-tex>
        </template>
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        label="资料照片">
        <template width="40" slot-scope="scope">
          <el-image :src="scope.row.image" style="width: 50px; height: 50px"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="本人联系方式" align="center" prop="selfPhone" />
      <el-table-column label="时长" align="center" prop="hour" />
      <el-table-column label="紧急联系方式" align="center" prop="backupPhone" />

      <el-table-column width="100" label="导员审批状态" align="center">
        <template slot-scope="scope">
          <el-tag type="warning" effect="dark" v-if="scope.row.approveStatusGuide==0">待审核</el-tag>
          <el-tag type="success" effect="dark" v-if="scope.row.approveStatusGuide==1">已通过</el-tag>
          <el-tag type="danger" effect="dark" v-if="scope.row.approveStatusGuide==2">未通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column width="100" label="领导审批状态" align="center">
        <template slot-scope="scope">
          <el-tag type="warning" effect="dark" v-if="scope.row.approveStatusLeading==0">待审核</el-tag>
          <el-tag type="success" effect="dark" v-if="scope.row.approveStatusLeading==1">已通过</el-tag>
          <el-tag type="danger" effect="dark" v-if="scope.row.approveStatusLeading==2">未通过</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:manage:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:manage:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看详情 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" :disabled="true" />
        </el-form-item>
        <el-form-item label="请假类型" prop="type">
          <template>
            <el-tex effect="dark" v-if="form.type==0">事假</el-tex>
            <el-tex effect="dark" v-if="form.type==1">病假</el-tex>
            <el-tex effect="dark" v-if="form.type==2">实习</el-tex>
            <el-tex effect="dark" v-if="form.type==2">其他</el-tex>
          </template>
        </el-form-item>
        <el-form-item label="健康状况" prop="healthCondition">
          <el-input v-model="form.healthCondition" placeholder="请输入健康状况" />
        </el-form-item>
        <el-form-item label="去往地" prop="destination">
          <el-input v-model="form.destination" placeholder="请输入去往地" />
        </el-form-item>
        <el-form-item label="资料照片">
          <imageUpload v-model="form.image"/>
        </el-form-item>
        <el-form-item label="本人联系方式" prop="selfPhone">
          <el-input v-model="form.selfPhone" placeholder="请输入本人联系方式" />
        </el-form-item>
        <el-form-item label="时长" prop="hour">
          <el-input v-model="form.hour" placeholder="请输入时长" />
        </el-form-item>
        <el-form-item label="紧急联系方式" prop="backupPhone">
          <el-input v-model="form.backupPhone" placeholder="请输入紧急联系方式" />
        </el-form-item>
        <el-form-item label="导员审批状态" prop="approveStatusGuide">
          <el-input v-model="form.approveStatusGuide" placeholder="请输入导员审批状态" />
        </el-form-item>
        <el-form-item label="导员审批人" prop="approveByGuide">
          <el-input v-model="form.approveByGuide" placeholder="请输入导员审批人" />
        </el-form-item>
        <el-form-item label="领导审批状态" prop="approveStatusLeading">
          <el-input v-model="form.approveStatusLeading" placeholder="请输入领导审批状态" />
        </el-form-item>
        <el-form-item label="领导审批人" prop="approveByLeading">
          <el-input v-model="form.approveByLeading" placeholder="请输入领导审批人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listManage, getManage, delManage, addManage, updateManage, exportManage } from "@/api/leave_manage/manage";
import ImageUpload from '@/components/ImageUpload';

export default {
  name: "Manage",
  components: {
    ImageUpload,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      //年级
      gradeOptions:[],
      //学院
      collegeOptions:[],
      //专业
      majorOptions:[],
      //请假类型
      typeOptions: [],
      //审核状态
      auditStatusOptions:[],
      // 总条数
      total: 0,
      // 请假信息表格数据
      manageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        grade: undefined,
        college: undefined,
        major: undefined,
        type: undefined,
        healthCondition: undefined,
        destination: undefined,
        image: undefined,
        selfPhone: undefined,
        hour: undefined,
        backupPhone: undefined,
        approveStatusGuide: undefined,
        approveByGuide: undefined,
        approveStatusLeading: undefined,
        approveByLeading: undefined,
        isDelete: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();

    /**
     * 年级查找
     */
    this.getDicts("lea_school_grade").then(response => {
      this.gradeOptions = response.data;
    });
    /**
     * 学院查找
     */
    this.getDicts("lea_school_college").then(response => {
      this.collegeOptions = response.data;
    });

    /**
     * 请假类型查找
     */
    this.getDicts("lea_leave_type").then(response => {
      this.typeOptions = response.data;
    });
    /**
     * 请假审核状态
     */
    this.getDicts("lea_leave_audit_status").then(response => {
      this.auditStatusOptions = response.data;
    });
  },
  methods: {
    /** 查询请假信息列表 */
    getList() {
      this.loading = true;
      listManage(this.queryParams).then(response => {
        this.manageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //查找专业
    onSelectedMajor(val) {
      /**
       * 专业查找
       */
      this.getDicts("lea_school_"+val+"_college_major").then(response => {
        this.majorOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        leaveId: undefined,
        userId: undefined,
        type: undefined,
        healthCondition: undefined,
        destination: undefined,
        image: undefined,
        selfPhone: undefined,
        hour: undefined,
        backupPhone: undefined,
        approveStatusGuide: undefined,
        approveByGuide: undefined,
        approveStatusLeading: undefined,
        approveByLeading: undefined,
        isDelete: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.leaveId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加请假信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const leaveId = row.leaveId || this.ids
      getManage(leaveId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改请假信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.leaveId != null) {
            updateManage(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addManage(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const leaveIds = row.leaveId || this.ids;
      this.$confirm('是否确认删除请假信息编号为"' + leaveIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delManage(leaveIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有请假信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportManage(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
